#!/usr/bin/env node

/**
 * HeaderInfo69x修复验证脚本
 * 验证按照58x和68x模式修复后的HeaderInfo69x.ets是否正确
 */

const fs = require('fs');
const path = require('path');

console.log('🔍 开始验证HeaderInfo69x修复结果...\n');

// 文件路径
const headerInfo69xPath = 'src/main/ets/components/plugin/SuotaLib/suota/HeaderInfo69x.ets';

// 检查文件是否存在
if (!fs.existsSync(headerInfo69xPath)) {
  console.log('❌ HeaderInfo69x.ets文件不存在');
  process.exit(1);
}

// 读取文件内容
const content = fs.readFileSync(headerInfo69xPath, 'utf8');

console.log('📄 检查HeaderInfo69x.ets修复结果...');

// 检查项目列表
const checks = [
  {
    name: '导入必要的依赖',
    pattern: /import.*SuotaByteBuffer.*from.*SuotaUtils/,
    required: true
  },
  {
    name: '导入hilog',
    pattern: /import.*hilog.*from.*@kit\.PerformanceAnalysisKit/,
    required: true
  },
  {
    name: '私有构造函数带参数',
    pattern: /private constructor\(\s*offsetPayloadSize: number,\s*offsetPayloadCrc: number,\s*offsetVersion: number,\s*versionLength: number,\s*offsetTimestamp: number\s*\)/,
    required: true
  },
  {
    name: 'initWithHeaderBuffer静态方法',
    pattern: /static initWithHeaderBuffer\(header: ArrayBuffer, totalBytes: number\): HeaderInfo69x/,
    required: true
  },
  {
    name: 'initWithRawBufferData静态方法',
    pattern: /static initWithRawBufferData\(rawBuffer: ArrayBuffer\): HeaderInfo69x/,
    required: true
  },
  {
    name: '创建69x特定实例',
    pattern: /const instance = new HeaderInfo69x\(/,
    required: true
  },
  {
    name: '复制父类实例属性',
    pattern: /instance\.header = baseInstance\.header;/,
    required: true
  },
  {
    name: '调用类型特定初始化',
    pattern: /instance\.initializeTypeSpecific\(\);/,
    required: true
  },
  {
    name: 'hilog日志记录',
    pattern: /hilog\.info\(0x0000, 'HeaderInfo69x'/,
    required: true
  },
  {
    name: '错误处理',
    pattern: /hilog\.error\(0x0000, 'HeaderInfo69x'/,
    required: true
  },
  {
    name: 'try-catch错误处理',
    pattern: /try\s*\{[\s\S]*\}\s*catch\s*\(error\)/,
    required: true
  },
  {
    name: '静态属性访问器TYPE',
    pattern: /static get TYPE\(\): string/,
    required: true
  },
  {
    name: '静态属性访问器SIGNATURE',
    pattern: /static get SIGNATURE\(\): number/,
    required: true
  },
  {
    name: '静态属性访问器HEADER_SIZE',
    pattern: /static get HEADER_SIZE\(\): number/,
    required: true
  }
];

let passedChecks = 0;
let totalChecks = checks.length;

checks.forEach(check => {
  const found = check.pattern.test(content);
  if (found) {
    console.log(`   ✅ ${check.name}`);
    passedChecks++;
  } else if (check.required) {
    console.log(`   ❌ ${check.name} - 必需但未找到`);
  } else {
    console.log(`   ⚠️  ${check.name} - 可选但未找到`);
  }
});

console.log('\n📊 检查结果统计:');
console.log(`   通过: ${passedChecks}/${totalChecks}`);
console.log(`   成功率: ${Math.round(passedChecks/totalChecks*100)}%`);

// 检查是否有旧的错误模式
console.log('\n🔍 检查是否还有旧的错误模式...');

const antiPatterns = [
  {
    name: '旧的无参构造函数',
    pattern: /private constructor\(\)\s*\{/,
    shouldNotExist: true
  },
  {
    name: '使用Object.assign复制属性',
    pattern: /Object\.assign\(instance, baseInstance\)/,
    shouldNotExist: true
  },
  {
    name: '简单的if检查buffer',
    pattern: /if \(this\.buffer\) \{[\s\S]*?\}/,
    shouldNotExist: false // 这个可能存在，但应该是更完善的版本
  }
];

let antiPatternIssues = 0;

antiPatterns.forEach(pattern => {
  const found = pattern.pattern.test(content);
  if (found && pattern.shouldNotExist) {
    console.log(`   ❌ 发现旧模式: ${pattern.name}`);
    antiPatternIssues++;
  } else if (!found && pattern.shouldNotExist) {
    console.log(`   ✅ 已移除旧模式: ${pattern.name}`);
  }
});

console.log('\n==================================================');

if (passedChecks === totalChecks && antiPatternIssues === 0) {
  console.log('🎉 HeaderInfo69x修复验证通过！');
  console.log('✅ 所有必需的模式都已正确实现');
  console.log('✅ 已按照58x和68x的模式进行修复');
  console.log('✅ 与iOS版本保持一致');
  process.exit(0);
} else {
  console.log('❌ HeaderInfo69x修复验证失败');
  if (passedChecks < totalChecks) {
    console.log(`❌ 缺少 ${totalChecks - passedChecks} 个必需的实现`);
  }
  if (antiPatternIssues > 0) {
    console.log(`❌ 发现 ${antiPatternIssues} 个旧的错误模式`);
  }
  console.log('\n💡 建议：');
  console.log('1. 检查是否按照HeaderInfo68x的模式实现');
  console.log('2. 确保所有静态方法都正确实现');
  console.log('3. 确保错误处理和日志记录完整');
  process.exit(1);
}